﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Nizm0.QOT.Library.Forms
{
    public partial class DescribeVersions : Form
    {
        int index = 0;

        Objects.ProcedureVersion _procVer = new Objects.ProcedureVersion();

        string _oryginalText;
        string[] _strTab;
        string[] _optionTab = { "Version", "Date", "Project", "User", "Comment" };
        List<string> _options;
        List<DescriptionOptionContainer> _selectedOptions = new List<DescriptionOptionContainer>();

        public DescribeVersions(string text)
        {
            _oryginalText = text;
            InitializeComponent();
            _options = new List<string>();
            _strTab = text.Split(ChoseSeparator(text));
            bt_Previous.Enabled = true;
            textBox1.Text = _strTab[index];
        }

        private char[] ChoseSeparator(string text)
        {
            char[] t = {'|'};
            return t;
        }

        public void Fill()
        {
            textBox1.Text = _strTab[index];
            comboBox1.DataSource = _options;

            if (index <= 0)
                bt_Previous.Enabled = false;
            else
                bt_Previous.Enabled = true;
            if (index <= _strTab.Length - 1)
            {
                bt_Next.Visible = true;
                bt_End.Visible = false;
            }
            else
            {
                bt_Next.Visible = false;
                bt_End.Visible = true;
            }
        }

        private void bt_Next_Click(object sender, EventArgs e)
        {
            DescribeElement();
            index++;
            Fill();
        }

        private void bt_Previous_Click(object sender, EventArgs e)
        {
            DescribeElement();
            index--;
            Fill();
        }

        private void bt_End_Click(object sender, EventArgs e)
        {
            Describe();
            this.Close();
        }

        private void Describe()
        {
            DescribeElement();
            _procVer._originalText = _oryginalText;
            _procVer.Version = _selectedOptions.FirstOrDefault(v => v.Field == "Version").Text;
            _procVer.Project = new Objects.ExternalProject(_selectedOptions.FirstOrDefault(v => v.Field == "Project").Text);
            _procVer.User = _selectedOptions.FirstOrDefault(v => v.Field == "User").Text;
            _procVer.Date = DateTime.Parse(_selectedOptions.FirstOrDefault(v => v.Field == "Date").Text);
            _procVer.Comment = _selectedOptions.FirstOrDefault(v => v.Field == "Comment").Text;
        }

        private void DescribeElement()
        {
            if (_selectedOptions.Exists(o => o.Field == comboBox1.Text))
            {
                MessageBox.Show("Field \"" + comboBox1.Text + "\" is assigned to \"" + _selectedOptions.Find(o => o.Field == comboBox1.Text).Text + "\"", "Worning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                if (_selectedOptions.Exists(o => o.Text == _strTab[index]))
                    _selectedOptions.Find(o => o.Field == comboBox1.Text).Field = comboBox1.Text;
                else
                    _selectedOptions.Add(new DescriptionOptionContainer(_strTab[index], comboBox1.Text));
            }
        }

        private void DescribeVersions_FormClosing(object sender, FormClosingEventArgs e)
        {
            Describe();
            if (_selectedOptions.Count != _strTab.Length)
                if (DialogResult.No == MessageBox.Show("Not all Fields have been assigned", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
                    e.Cancel = true;
        }
    }

    public class DescriptionOptionContainer
    {
        public string Text;
        public string Field;

        public DescriptionOptionContainer(string Text, string Field)
        {
            this.Text = Text;
            this.Field = Field;
        }
    }
}
